package com.linux.huhx.concurreny;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class Driver1 {
    public static void main(String[] args) throws Exception {
        CountDownLatch startSignal = new CountDownLatch(1);
        CountDownLatch doneSignal = new CountDownLatch(5);
        for (int i = 0; i < 5; ++i) // create and start threads
            new Thread(new TaskWorker(startSignal, doneSignal)).start();
        System.out.println("countDown before.");           // don't let run yet
        startSignal.countDown();      // let all threads proceed
        TimeUnit.SECONDS.sleep(1);
        System.out.println("await before");
        /**
         * 1、由于doneSignal里面的初始count=5，所以主线程在此会阻塞。
         * 2、当所有的子线程执行完（doneSignal.countDown()）之后，此时的count=0。所以后面代码可以继续执行。
         */
        doneSignal.await();
        System.out.println("await after");
    }

    private static class TaskWorker implements Runnable {
        private final CountDownLatch startSignal;
        private final CountDownLatch doneSignal;

        public TaskWorker(CountDownLatch startSignal, CountDownLatch doneSignal) {
            this.startSignal = startSignal;
            this.doneSignal = doneSignal;
        }

        @Override
        public void run() {
            try {
                /**
                 * 1、刚进来的时候，由于startSignal的count=1。所以await()方法阻塞于此。
                 * 2、当主线程执行了startSignal.countDown();此时的startSignal的count=0。所以子线程可以正常运行。
                 */
                startSignal.await();
                doSomeWork();
                doneSignal.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        private void doSomeWork() {
            System.out.println("in run method: " + Thread.currentThread().getName());
        }
    }
}
